package com.gy.controller;

import com.gy.entity.Question;
import com.gy.entity.User;
import com.gy.serivce.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    private DataService dataService;

    @GetMapping({"/list"})
    public String questionList(Model model) {
        List<Question> list = dataService.selectAllQuestion();
        List<Question> androidList = dataService.selectAllAndroidQuestion();
        list.addAll(androidList);
        // TODO 这里需要查询Android表进行一个整合
        model.addAttribute("list", list);
        return "list";
    }

    @GetMapping({"/index.html", "/"})
    public String toLogin(Model model) {
        return "login";
    }

    @PostMapping("/admin/login")
    @ResponseBody
    public Map<String, Integer> adminLogin(User user, Model model) {
        // 执行登录逻辑
        User admin = dataService.adminLogin(user.getUsername(), user.getPassword());
        HashMap<String, Integer> map = new HashMap<>();
        if (admin != null) {
            map.put("code", 0);
        } else {
            map.put("code", 1);
        }
        return map;
    }


    @GetMapping("/add.html")
    public String toAdd(Model model) {
        return "add";
    }

    @PostMapping("/add")
    public String addQuest(Question question) {
        if (question.getType().split("_")[2].equals("java")) {
            // 默认添加到t_question表
            dataService.add(question);
        } else {
            // 添加到t_android表
            dataService.addToAndroid(question);
        }
        return "redirect:/list";
    }


    @RequestMapping("/delete/{id}/{type}")
    public String delete(@PathVariable Integer id, @PathVariable String type) {
        if (type.split("_")[2].equals("java")) {
            dataService.delete(id);
        } else {
            dataService.deleteToAndroid(id);
        }
        return "redirect:/list";
    }

    /**
     * 跳转到更新页面的需要进行一次查询
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/update/{id}/{type}")
    public String toUpdate(@PathVariable Integer id, Model model, @PathVariable String type) {
        Question question = new Question();
        if (type.split("_")[2].equals("java")) {
            question = dataService.searchQuestById(id);
        } else {
            question = dataService.searchAndroidQuestById(id);
        }
        model.addAttribute("question", question);
        return "update";
    }

    /**
     * 修改后进行保存
     * @param question
     * @return
     */
    @PostMapping("/save")
    public String save(Question question) {
        if (question.getType().split("_")[2].equals("java")) {
            dataService.save(question);
        } else {
            dataService.saveToAndroid(question);
        }
        return "redirect:/list";
    }
}
